1873F - Money Trees - CodeForces Solution


greedy greedy greedy math two pointers

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
const int N = 1e5 + 10;
typedef long long ll;
#define int long long
#define endl '\n'
#define INF 0x7fffffff
#define fi first
#define se second
#define pii pair<int, int>
#define YES                                                                                                            \
    {                                                                                                                  \
        cout << "YES" << endl;                                                                                         \
        return;                                                                                                        \
    }
#define NO                                                                                                             \
    {                                                                                                                  \
        cout << "NO" << endl;                                                                                          \
        return;                                                                                                        \
    }
 
using namespace std;
 
// struct node
// {
//
// };
//
// bool cmp(const node& x,node& y)
// {
//     return x.<y.;
// }
 
void solve()
{
    int n, m;
    cin >> n >> m;
    vector<int> a(n + 1, INT_MAX);
    vector<int> b(n + 1, 0);
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= n; i++)
    {
        cin >> b[i];
    }
 
    vector<pair<int, int>> p;
 
    for (int i = 1; i <= n; i++)
    {
        if (i + 1 <= n && b[i] % b[i + 1] == 0)
        {
            int j = i;
            while (j + 1 <= n && b[j] % b[j + 1] == 0)
            {
                j++;
            }
            p.push_back({i, j});
            i = j;
        }
    }
    int len = 0;
    for (auto x : p)
    {
        int l = x.first, r = x.first;
        int temp = a[r];
        while (r <= x.second)
        {
            if (temp <= m)
                len = max(len, r - l + 1);
            r++;
            if (r <= x.second)
                temp += a[r];
            else
                break;
            while (temp > m && l <= r)
            {
                temp -= a[l];
                l++;
            }
        }
 
        if (temp <= m)
            len = max(len, r - l);
    }
    if (len == 0)
    {
    	int x = *min_element(a.begin(),a.end());
        if (x <= m) cout<<1<<endl;
        else cout<<0<<endl;
    }
    else
    {
        cout << len << endl;
    }
}
 
signed main()
{
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(0);
    int __ = 1;
    cin >> __;
    while (__--)
        solve();
}


Comments

Submit
0 Comments
More Questions

1574A - Regular Bracket Sequences
1574B - Combinatorics Homework
1567A - Domino Disaster
1593A - Elections
1607A - Linear Keyboard
EQUALCOIN Equal Coins
XOREQN Xor Equation
MAKEPAL Weird Palindrome Making
HILLSEQ Hill Sequence
MAXBRIDGE Maximise the bridges
WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array